package com.ruoyi.web.controller.baodao.controller;

import com.ruoyi.baodao.entity.*;
import com.ruoyi.baodao.mapper.*;
import com.ruoyi.baodao.vo.GoodsInfoVO;
import com.ruoyi.baodao.vo.JDTJFileVO;
import com.ruoyi.baodao.vo.JDTJVO;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.web.controller.baodao.service.JDTJService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/jd")
public class JDController extends BaseController  {

    @Autowired
    JDTJGlobalMapper jdtjGlobalMapper;
    @Autowired
    JDTJClassMapper jdtjClassMapper;
    @Autowired
    JDTJGoodMapper jdtjGoodMapper;
    //@Autowired
    //GoodsClassMapper goodsClassMapper;

    @Autowired
    BDMapper bdMapper;

    @Autowired
    JDTJMapper jdtjMapper;



    @GetMapping("/global")
    public String global() { return   "baodao/jd/global"; }

    @GetMapping("/class")
    public String jdClass() { return   "baodao/jd/class"; }

    @GetMapping("/goods")
    public String user()
    {
        return   "baodao/jd/goods";
    }


    @GetMapping("/global/globalDetails/{id}")
    public String globalDetails(@PathVariable("id") String id, Model modelMap,String classCode,String type)
    {
        logger.info("请求参数： {}",id);
        modelMap.addAttribute("fileName",id);
        modelMap.addAttribute("classCode",classCode);
        modelMap.addAttribute("type",type);
        return "baodao/jd/globalDetails";
    }

    @GetMapping("/goods/goodsDetails/{id}")
    public String goodsCodeDetails(@PathVariable("id") String id, Model modelMap)
    {
        logger.info("请求参数： {}",id);
        modelMap.addAttribute("goodsCode",id);
        return "baodao/jd/goodsDetails";
    }

    // 京东推荐商品列表
    @PostMapping("/file/list")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public TableDataInfo filelist(JDTJ jdtj){
        //logger.info("startTime: {}, endTime: {}, p1: {}, p2: {}" , startTime,endTime,p1,p2);
        startPage(); //分页jd/goods
        //  List<JDTJ> list = jdtjService.list();
        List<JDTJFileVO> list = bdMapper.queryJDTJFileByStatus();
        return getDataTable(list);
    }

    /** global 弹出框 列表 **/
    @PostMapping("/global/JDTJDetails/list/{id}")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public TableDataInfo globalDetailsList(@PathVariable("id") String id)
    {
        logger.info("参数： " + id);
        startPage();
        //List<JDTJ> list = jdtjService.JDTJList();
        JDTJExample jdtjExample = new JDTJExample();
        JDTJExample.Criteria criteria = jdtjExample.createCriteria();
        criteria.andFileNameEqualTo(id);
        List<JDTJ> list = jdtjMapper.selectByExample(jdtjExample);
        return getDataTable(list);
    }

    /** global 弹出框 列表 **/
    @PostMapping("/goods/Detail/list/{id}")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public TableDataInfo goodsDetailsList(@PathVariable("id") String id)
    {
        logger.info("参数： " + id);
        startPage();
        //List<JDTJ> list = jdtjService.JDTJList();
        JDTJExample jdtjExample = new JDTJExample();
        JDTJExample.Criteria criteria = jdtjExample.createCriteria();
        criteria.andFileNameEqualTo(id);
        criteria.andStatusEqualTo("1");
        List<JDTJ> list = jdtjMapper.selectByExample(jdtjExample);
        return getDataTable(list);
    }



    /** global 推荐 列表 **/
    @PostMapping("/global/list")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public TableDataInfo globalList()
    {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        startPage();
        List<JDTJVO> list =  bdMapper.queryJDTJGlobalList(shopId);
        return  getDataTable(list);
    }

    // 列别 列表
    @PostMapping("/class/list")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public TableDataInfo classList(String classCode){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        startPage();
        List<JDTJVO> list =  bdMapper.queryJDTJClassList(shopId,classCode);
        return  getDataTable(list);

    }


    // 根据商品编码 查询 京东推荐商品列表
    @PostMapping("/goodsCode")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo queryJDTJByGoodSCode(String goodsCode){
        //log.info("根据商品编码查询京东推荐商品列表,请求参数：{} " ,goodsCode );
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        List<JDTJVO> list = bdMapper.queryJDTJGoodsList(shopId,goodsCode);
        //log.info("根据商品编码查询京东推荐商品列表,请求参数：{}, 查询结果：{} " ,goodsCode,list );
        return  getDataTable(list);

    }


    // 全局  增
    // 参数： 京东推荐表 表主键
    @PostMapping("/global/add")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public AjaxResult golbalAdd(Integer id){
        logger.info("京东推荐-全局, 参数： {}",id);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        JDTJGlobal jdtjGlobal = new JDTJGlobal();
        jdtjGlobal.setCreateTime(new Date());
        jdtjGlobal.setShopId(shopId);
        jdtjGlobal.setJdtjId(id);
        jdtjGlobal.setStatus("1");  // 插入1

        int result = jdtjGlobalMapper.insert(jdtjGlobal);
        if(result > 0) {
            logger.info("京东推荐-全局, 参数： {}, 插入成功",id);
            return success();
        }else{
            logger.info("京东推荐-全局, 参数： {}, 插入失败 ",id);
            return error();
        }



    }

    // 全局 删
    // 参数： 京东推荐表 表主键
    @PostMapping("/global/delete/{id}")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public AjaxResult golbalDelete(@PathVariable("id") Integer id){
        logger.info("京东推荐-全局, 参数： {}",id);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();


        JDTJGlobalExample jdtjGlobalExample = new JDTJGlobalExample();
        JDTJGlobalExample.Criteria criteria = jdtjGlobalExample.createCriteria();
        criteria.andIdEqualTo(id);
        //criteria.andShopIdEqualTo(shopId);
        JDTJGlobal jdtjGlobal = new JDTJGlobal();
        // jdtjGlobal.setCreateTime(new Date());
        // jdtjGlobal.setShopId(shopId);
        jdtjGlobal.setId(id);
        jdtjGlobal.setStatus("0");  // 插入1

        int result = jdtjGlobalMapper.updateByExampleSelective(jdtjGlobal,jdtjGlobalExample);
        if(result > 0) {
            logger.info("京东推荐-全局, 参数： {}, 插入成功",id);
            return success();
        }else{
            logger.info("京东推荐-全局, 参数： {}, 插入失败 ",id);
            return error();
        }

    }


    // 分类 增
    @PostMapping("/class/add")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    // 参数： 京东推荐表 表主键 , 药品类别
    public AjaxResult classAdd(Integer id,String classCode){
        logger.info("京东推荐-全局, 参数1： {}, 参数2：{} ",id,classCode);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        JDTJClass jdtjClass = new JDTJClass();
        jdtjClass.setCreateTime(new Date());
        jdtjClass.setJdtjId(id);
        jdtjClass.setStatus("1");
        jdtjClass.setClasscode(classCode);
        jdtjClass.setShopId(shopId);

        int result = jdtjClassMapper.insert(jdtjClass);
        if(result > 0) {
            logger.info("京东推荐-全局, 参数： {}, 插入成功",id);
            return success();
        }else{
            logger.info("京东推荐-全局, 参数： {}, 插入失败 ",id);
            return error();
        }

    }



    // 分类 删
    @PostMapping("/class/delete")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    // 参数： 京东推荐表 表主键 , 药品类别
    public AjaxResult classDelete(Integer id, String classCode){
        logger.info("京东推荐-全局, 参数1： {}, 参数2：{} ",id,classCode);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        JDTJClassExample jdtjClassExample = new JDTJClassExample();
        JDTJClassExample.Criteria criteria = jdtjClassExample.createCriteria();
        criteria.andClasscodeEqualTo(classCode);
        criteria.andJdtjIdEqualTo(id);
        JDTJClass jdtjClass = new JDTJClass();
        jdtjClass.setJdtjId(id);
        jdtjClass.setStatus("0");
        jdtjClass.setClasscode(classCode);
        jdtjClass.setShopId(shopId);

        int result = jdtjClassMapper.updateByExampleSelective(jdtjClass,jdtjClassExample);
        if(result > 0) {
            logger.info("京东推荐-全局, 参数： {}, 插入成功",id);
            return success();
        }else{
            logger.info("京东推荐-全局, 参数： {}, 插入失败 ",id);
            return error();
        }
    }




    //商品 增
    @GetMapping("/goods/add")
    public void goodsAdd(){

    }


    //商品 删
    @PostMapping("/goods/delete")
    @DataSource(value = DataSourceType.SLAVE)
    @ResponseBody
    public AjaxResult goodsDelete(Integer id){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        JDTJGoodExample jdtjGoodExample = new JDTJGoodExample();
        JDTJGoodExample.Criteria criteria = jdtjGoodExample.createCriteria();
        //criteria.andGoodsCodeEqualTo(goodsCode);
        criteria.andIdEqualTo(id);
        JDTJGood jdtjGood = new JDTJGood();
        //jdtjGood.setId(id);
        jdtjGood.setStatus("0");
        //jdtjGood.setGoodsCode(goodsCode);
        //jdtjGood.setShopId(shopId);


        //int result = jdtjGoodMapper.updateByPrimaryKey(jdtjGood);
        int result = jdtjGoodMapper.updateByExampleSelective(jdtjGood,jdtjGoodExample);
        if(result > 0) {
            logger.info("京东推荐-全局, 参数： {}, 插入成功",id);
            return success();
        }else{
            logger.info("京东推荐-全局, 参数： {}, 插入失败 ",id);
            return error();
        }

    }



    // 查询商品分类
    @PostMapping("/queryGoodsClass")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo queryGoodsClass(){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        List<GoodsClass> list = bdMapper.queryJDTJClassNumList(shopId);
        return  getDataTable(list);
    }


    // 根据 数组 查询 商品信息
    @PostMapping("/saveGoodsByGoodsCodes")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    // key1 商品编号  key 京东推荐 主键
    @Transactional
    public AjaxResult saveGoodsByGoodsCodes(String[] key1,String [] key2){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();

        // 先删除

        Date date = new Date();
        for (int i = 0; i < key1.length ; i++) {

            // where 条件
            JDTJGoodExample jdtjGoodExample = new JDTJGoodExample();
            JDTJGoodExample.Criteria criteria = jdtjGoodExample.createCriteria();
            criteria.andGoodsCodeEqualTo(key1[i]); // goodsCode

            // update 内容
            JDTJGood jdtjGoodDel = new JDTJGood();
            jdtjGoodDel.setStatus("0");

            // 先删除再插入
            jdtjGoodMapper.updateByExampleSelective(jdtjGoodDel,jdtjGoodExample);

            for (int j = 0; j < key2.length; j++) {
                JDTJGood jdtjGood = new JDTJGood();
                jdtjGood.setCreateTime(date);
                jdtjGood.setShopId(shopId);
                jdtjGood.setGoodsCode(key1[i]);
                jdtjGood.setStatus("1");
                jdtjGood.setJdtjId(Integer.valueOf(key2[j]));
                jdtjGoodMapper.insert(jdtjGood);
            }
        }
        return  success();
    }

    // 根据 数组 查询 商品信息
    @PostMapping("/queryGoodsByGoodsCodes")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo queryGoodsByJDTJId(String ids){
        startPage();

        String[] jdIds=ids.split(",");
        Long[] key1 = Stream.of(jdIds).map(s -> Long.valueOf(s)).collect(Collectors.toList()).toArray(new Long[]{});



        JDTJExample jdtjExample = new JDTJExample();
        JDTJExample.Criteria criteria = jdtjExample.createCriteria();
        List<Long> list = new ArrayList<>();
        list = Arrays.asList(key1);
        criteria.andIdIn(list);
        List<JDTJ> jdtjs =  jdtjMapper.selectByExample(jdtjExample);
        // List<GoodsClass> list = goodsClassMapper.selectByExample(new GoodsClassExample());
        return  getDataTable(jdtjs);
    }



    // 京东推荐-商品页面 药品信息
    @PostMapping("/queryGoodsInfoForJD")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo queryGoodsByJDTJId(MFGoods9999  mfGoods9999){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        String shopId = sysUser.getDept().getDeptName();
        startPage();
        List<GoodsInfoVO> list = bdMapper.queryGoodsForJD(mfGoods9999.getGoodscode(),mfGoods9999.getGoodsname(),shopId,mfGoods9999.getClassname(),null,null);
        return  getDataTable(list);
    }


    // 京东推荐-商品页面  移除选定药品所有 关联 京东推荐链接
    @PostMapping("/removeJDTJByGoodsCodes")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    @Transactional
    public AjaxResult removeJDTJByGoodsCodes(String[] key1){

        List<String> list = new ArrayList<>();
        list = Arrays.asList(key1);
        JDTJGoodExample jdtjGoodExample = new JDTJGoodExample();
        JDTJGoodExample.Criteria criteria = jdtjGoodExample.createCriteria();
        criteria.andGoodsCodeIn(list);
        JDTJGood jdtjGood = new JDTJGood();
        jdtjGood.setStatus("0");
        int result = jdtjGoodMapper.updateByExampleSelective(jdtjGood,jdtjGoodExample);
        if(result > 0 )
            return success();
        else
            return error();
    }

}
